(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(*-----------------------------------------------------------------------*)

open FontForge
open Pycaml

(*-----------------------------------------------------------------------*)

module Tag :
sig
  type t = int32 (* with sexp *) (* FIX: Do we still need to expose the type? *)
  val make_from_ints : int array -> t
  val make     : string -> t
  val str      : t -> string    
  val compare  : t -> t -> int
end

module TagMap : Nibbletrie.S with type key = Tag.t

(*-----------------------------------------------------------------------*)

(* Glyph indexes *)

module GID :
sig
  type t = int (* with sexp *)
  external of_int : int -> t = "%identity"
  external to_int : t -> int = "%identity"
  val compare : t -> t -> int
  val succ    : t -> t
  val print   : t -> unit
  val pythonize   : t -> pyobject
  val unpythonize : pyobject -> t
end

module GIDMap : Nibbletrie.S with type key = GID.t
module GIDSet : Nibbleset.S with type elt = GID.t

type value_record = {
  (* Device-table fields are not supported. *)
  x_placement_adj : int;
  y_placement_adj : int;
  x_advance_adj   : int;
  y_advance_adj   : int;
} (* with sexp *)

type lookup_type =

  | DummyLookup

    (* GPOS *)
  | SingleAdjustmentLookup
  | PairAdjustmentLookup
  | CursiveAttachmentLookup
  | MarkToBaseAttachmentLookup
  | MarkToLigatureAttachmentLookup
  | MarkToMarkAttachmentLookup
  | ContextPositioningLookup
  | ChainedContextPositioningLookup
  | ExtensionPositioningLookup

      (* GSUB *)
  | SingleSubstitutionLookup
  | MultipleSubstitutionLookup
  | AlternateSubstitutionLookup
  | LigatureSubstitutionLookup
  | ContextSubstitutionLookup
  | ChainingContextSubstitutionLookup
  | ExtensionSubstitutionLookup
  | ReverseChainingContextSubstitutionLookup

(* with sexp *)

type chaining_context_subst_format3 = {
  backtrack_coverage : GIDSet.t list;
  input_coverage     : GIDSet.t list;
  lookahead_coverage : GIDSet.t list;
  lookup_indexes     : (int * int) list;
}
and subtable =
    | SingleSubstitutionSubtable of GID.t GIDMap.t
    | LigatureSubstitutionSubtable of (GID.t list * GID.t) list GIDMap.t
    | ChainingContextSubstitution3Subtable of chaining_context_subst_format3
    | SingleAdjustmentSubtable of value_record GIDMap.t
    | PairAdjustmentSubtable of (value_record option * value_record option) GIDMap.t GIDMap.t
    | ClassBasedKerningSubtable of (int * int GIDMap.t * int GIDMap.t * int array)

and lookup_table = {
  lookupType          : lookup_type;
  rightToLeft         : bool;
  ignoreBaseGlyphs    : bool;
  ignoreLigatures     : bool;
  ignoreMarks         : bool;
  useMarkFilteringSet : bool;
  markAttachmentType  : int;
  subTables           : subtable list;
}
(* with sexp *)

(* FIX: Feature parameters are not yet implemented. *)
type feature_parameters = NoFeatureParameters (* with sexp *)

type feature_table = {
  featureTag        : Tag.t;
  featureParams     : feature_parameters;
  lookupListIndexes : int list;
} (* with sexp *)

(* FIX: Re-ordering tables are not yet implemented. *)
type reordering_table = NoReorderingTable (* with sexp *)

type langsys_table = {
  lookupOrder : reordering_table;
  reqFeature  : feature_table option;
  features    : feature_table list;
} (* with sexp *)

type gsub_or_gpos = {
  langSysTable    : langsys_table;
  lookupListArray : lookup_table array;
} (* with sexp *)

val make_langsys_restricter :
  FFfont.t -> Tag.t -> Tag.t -> Tag.t * Tag.t

module LookupMarks :
sig
  type t (* with sexp *)
  val make_from_indexes : int -> int list -> t
  val indexes   : t -> int list
  val union     : t -> t -> t
end

module LookupMarker :
sig
  type t (* with sexp *)
  val make_gsub : FFfont.t -> Tag.t -> Tag.t -> t
  val make_gpos : FFfont.t -> Tag.t -> Tag.t -> t
  val make_from_table : gsub_or_gpos option -> t
  val dummy     : t
  val marks     : t -> Tag.t list -> LookupMarks.t
  val lookups   : t -> lookup_table array
end

module MarkerRepository :
sig
  type t (* with sexp *)
  val create    : unit -> t
  val put       : t -> LookupMarker.t -> Tag.t -> Tag.t -> unit
  val put_gsub  : t -> FFfont.t -> Tag.t -> Tag.t -> unit
  val put_gpos  : t -> FFfont.t -> Tag.t -> Tag.t -> unit
  val get       : t -> Tag.t -> Tag.t -> LookupMarker.t
end

(*-----------------------------------------------------------------------*)

exception Bad_FeatureSet_spec of string

module FeatureSet :
sig
  type t (* with sexp *)
  val empty      : t
  val default    : t
  val change_one : t -> string -> t
  val change     : t -> string list -> t
  val marks      : t -> LookupMarker.t -> LookupMarks.t
end

(*-----------------------------------------------------------------------*)
